<?php
namespace admin\controllers;


use admin\models\UserIll;
use Yii;
use yii\data\Pagination;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

/**
 * Created by PhpStorm.
 * User: 沈小鱼
 * Date: 2016/7/26
 * Time: 19:37
 */
class IllController extends BaseController{



    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['add','list','del'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                ],
            ],
        ];
    }



    public function actionList(){
        $user_id = Yii::$app->request->get('user_id');
        $data = UserIll::find()->where('mu_id='.$user_id);
        $illPages = new Pagination([
            'totalCount' =>$data->count(),
            'pageSize' => '5',
            'pageSizeParam' => false,
        ]);
        $ills = $data->offset($illPages->offset)->limit($illPages->limit)->all();
        return $this->renderPartial('list',[
            'illPage'=>$illPages,
            'ills'=>$ills,
        ]);
    }


    public function actionAdd(){
        $admin = Yii::$app->user->identity->getId();
        if(empty($admin)){
            return $this->showResult(302,'用户已登出');
        }
        $user_id = Yii::$app->request->get('id');
        if(empty($user_id)){
            return $this->showResult(301,'读取用户信息失败');
        }
        $illArr = Yii::$app->request->post('UserIll');
        if(empty($illArr['mi_id'])){
            $ill = new UserIll();
            $ill->attributes = [
                'mi_type'=>$illArr['mi_type'],
                'mu_id'=>$user_id,
                'mi_cure'=>$illArr['mi_cure'],
            ];
        }else{
            $ill = UserIll::findOne($illArr['mi_id']);
            $ill->attributes = [
                'mi_type'=>$illArr['mi_type'],
                'mu_id'=>$user_id,
                'mi_cure'=>$illArr['mi_cure'],
            ];
        }
        if(!$ill->save()){
            return $this->showResult(400,'保存失败');
        }else{
            return $this->showResult(200,'保存成功');
        }
    }

    public function actionDel(){
        $mi_id = Yii::$app->request->post('mi_id');
        if(empty($mi_id)){
            return $this->showResult(301,'未获取到病历信息');
        }
        $admin = Yii::$app->user->identity;
        if(empty($admin)){
            return $this->showResult(302,'您的登录信息已失效，请重新登录');
        }
        $ill = UserIll::findOne($mi_id);
        if(empty($ill)){
            return $this->showResult(301,'该条记录已不存在');
        }
        $row = Yii::$app->db->createCommand('delete from mm_user_ill WHERE mi_id='.$mi_id)->execute();
        if(!empty($row)){
            return $this->showResult(200,'删除成功');
        }else{
            return $this->showResult(400,'删除失败');
        }
    }

}